/*
 * @Author: chenli chenli@kcwl.com
 * @Date: 2023-07-14 16:05:47
 * @LastEditors: chenli chenli@kcwl.com
 * @LastEditTime: 2023-07-28 10:54:23
 * @FilePath: \jsnode\typescript\decorator\parDecorator.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import "reflect-metadata";
{
  const ParDec: ParameterDecorator = (
    target: Object,
    propertyKey: string | symbol | undefined,
    parameterIndex: number
  ) => {
    console.log("参数装饰器");

    let params: number[] = [];
    params.push(parameterIndex);
    Reflect.defineMetadata(
      "mykey",
      params,
      target,
      propertyKey!
    );
  };
  const MetDec: MethodDecorator = (
    target: Object,
    propertyKey: string | symbol,
    descriptor: any
  ) => {
    console.log("方法装饰器");
    const lodFun = descriptor.value
    descriptor.value = function(){
      const reqParams:number[] = Reflect.getMetadata("mykey", target, propertyKey!)
      console.log('参数长度:',arguments.length);
      reqParams.forEach(item=>{
        if(arguments[item].length>10){
          // lodFun(...arguments)
          throw new Error('名字太长了:'+arguments[item])
        }
      })
    }
  };

  class Laowang {
    @MetDec
    public getName(
       first: string,
       @ParDec lastName: string,
    ) {
      console.log(first, lastName);
    }
  }
  let laowang = new Laowang();
  laowang.getName("张", "三1234567890");
}
